//
//  MOWorkJiShiViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/9/19.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOWorkJiShiViewController.h"
#import "JLCalendarScrollView.h"
#import "MOWorkJiShiDetailOrAddViewController.h"

@interface MOWorkJiShiViewController ()<JLCalendarItemDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)JLCalendarScrollView *riLiView;

@property(nonatomic,strong)NSString *dateStr;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSDictionary *allDataDic;

@property(nonatomic,strong)NSArray *dataArray;

@property(nonatomic,strong)NSString *currentSelectedDate;

@end

@implementation MOWorkJiShiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNav];
    
    [self creatView];
    
    [self initData];
    
    [self getData:0];

}


-(void)initNav
{
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    [addButton setImage:IMAGE(@"use_add") forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(clickAddTask) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

-(void)creatView
{
    self.view.backgroundColor = KGrayColor_f8f8f8;
    
    self.title = @"工作纪实";
    
    self.riLiView = [[JLCalendarScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 450)];
    self.riLiView.delegate = self;
    self.riLiView.currentDay = [[NSString getCurrentTimesAndFormat:@"dd"] integerValue];
    self.riLiView.currentMonth = [[NSString getCurrentTimesAndFormat:@"MM"] integerValue];
    self.riLiView.currentYear = [[NSString getCurrentTimesAndFormat:@"YYYY"] integerValue];
    [self.view addSubview:self.riLiView];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.riLiView.bottom + 10, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - self.riLiView.height - 10) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = KGrayColor_f8f8f8;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

-(void)initData
{
    self.dateStr = [NSString getCurrentTimesAndFormat:@"YYYY-MM-dd"];
    self.currentSelectedDate = [NSString getCurrentTimesAndFormat:@"YYYY-MM-dd"];
}

-(void)getData:(NSInteger)index
{
    self.riLiView.haveDataArray = @[];
    self.dataArray = @[];
    [self.tableView reloadData];
    
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",SZRSGZJS_QUERYGZJSLIST_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"date":self.dateStr
                          };
    [PPNetworkHelper POST:url parameters:dic success:^(id responseObject) {
        NSLog(@"工作纪实列表：%@",responseObject);
        if ([responseObject[@"todoDateList"] isKindOfClass:[NSArray class]]) {
            NSArray *dateArr = responseObject[@"todoDateList"];
            self.riLiView.haveDataArray = dateArr;
        }else{
            self.riLiView.haveDataArray = @[];
        }
        
        if ([responseObject[@"todoItemList"] isKindOfClass:[NSDictionary class]]) {
            self.allDataDic = responseObject[@"todoItemList"];
            if (index==0) {
                NSString *dayS =  [NSString getCurrentTimesAndFormat:@"dd"];
                if ([self.allDataDic.allKeys containsObject:dayS]) {
                    self.dataArray = self.allDataDic[dayS];
                    [self.tableView reloadData];
                }else{
                    self.dataArray = @[];
                    [self.tableView reloadData];
                }
            }
        }
        
    } failure:^(NSError *error) {
        NSLog(@"工作纪实列表：%@",[error localizedDescription]);
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}


#pragma mark -- 添加
-(void)clickAddTask
{
    MOWorkJiShiDetailOrAddViewController *vc = [[MOWorkJiShiDetailOrAddViewController alloc]init];
    if (self.dataArray.count>0) {
        vc.idStr = self.dataArray[0][@"oid"];
        vc.dataDic = self.dataArray[0];
        vc.dateStr = self.currentSelectedDate;
    }else{
        vc.dateStr = self.currentSelectedDate;
    }
    __weak typeof (self)weakSelf = self;
    vc.saveSuceessBlock = ^{
        [weakSelf getData:0];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)seletedOneDay:(NSInteger)day withMonth:(NSInteger)month withYear:(NSInteger)year
{
    NSLog(@"%li-%li-%li", year,month,day);
    self.currentSelectedDate =  [NSString stringWithFormat:@"%ld-%02ld-%02ld",(long)year,(long)month,(long)day];
    NSString *dayS =  [NSString stringWithFormat:@"%ld",(long)day];
    if ([self.allDataDic.allKeys containsObject:dayS]) {
        self.dataArray = self.allDataDic[dayS];
        [self.tableView reloadData];
    }else{
        self.dataArray = @[];
        [self.tableView reloadData];
    }
}

-(void)monthOnclick:(NSInteger)lastOrNext date:(NSString *)date
{
    if (lastOrNext) {
        NSLog(@"下一个月 %@",date);
    }else{
        NSLog(@"上一个月 %@",date);
    }
    self.dateStr = date;
    [self getData:1];
}

-(void)yearOnclick:(NSInteger)lastOrNext date:(NSString *)date
{
    if (lastOrNext) {
        NSLog(@"下一个年 %@",date);
    }else{
        NSLog(@"上一个年 %@",date);
    }
    self.dateStr = date;
    [self getData:1];
}



#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.dataArray.count>0) {
        return 1;
    }
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = KBlackColor_222222;
        cell.textLabel.font = FONT_SIZE(15);
        cell.imageView.image = IMAGE(@"jiShi_left");
    }
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.textLabel.text = dic[@"gzjsnr"];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(50);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWorkJiShiDetailOrAddViewController *vc = [[MOWorkJiShiDetailOrAddViewController alloc]init];
    vc.idStr = self.dataArray[indexPath.row][@"oid"];
    vc.dataDic = self.dataArray[indexPath.row];
    __weak typeof (self)weakSelf = self;
    vc.saveSuceessBlock = ^{
        [weakSelf getData:0];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

@end
